define([
	'../libs/zepto',
	'../common/base',
	'../common/common_click',
	'../common/dialog',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/ajax',
	'../common/yingSoftSetTest'
],function(){
	window.total_count = 0;
	this.init = function(){
		setCss() // 设置样式
		_clickEvent();
		$('.exam_ullist').html('');
		var value = GetQueryString('keyWord');
		TestListUI(value);
	}

	
	
	// 设置样式
	function setCss(){
		var whole_height = window.innerHeight;
		var head_height = $('.common_header').height();
		
		// 搜索试题
		$('.search_exam').css('height',whole_height - head_height);
		$('#loadingDiv').css('display','block');
		$('.noSearch').css('display','none');
	}
	
	function _clickEvent(){
		$('.back').click(function(){
			window.location.href = '/doExam-index';
		});
	}
	// 搜索的试题列表
	function TestListUI(keyWord){
	    var _curPage = parseInt(G_Prg.getQueryString('page'));//强制转换成整型
	    if (!_curPage) {
	        _curPage = 1;
	    }else{
	    	_curPage = 1;
	    }
	    var isClickDouble = true; //防止多次点击
	    var _keyWord = keyWord;
	    var _showTestCount = 50;
	    var _totalPages = 0;
	    var _show = [];
	    var _allTest = '';
	    var _appEName = G_Cookie.getAppEName();
		
		if(_curPage == 1){
			_showListPage(); // 添加内容
		}
	    
	    function _showListPage() {
	      $('#pullUp').css('display','none');
	      $('#loadingDiv').css('display','block');
	        setTimeout(function () {
	        	var appEName = G_Cookie.getAppEName();
	        	var userName = G_Cookie.getUserName();
        		var userID = G_Cookie.getUserID();
        		var num = 0;
        		var exams = _getSearchExamJson(appEName, decodeURI(_keyWord), _curPage, userName, userID);
        		if(exams == '没有找到相关试题'){
        			num = 0;
        			$('#pullUp').css('display','none');
        			$('#loadingDiv').css('display','none');
        			$('.noSearch').css('display','block');
        			$('.title').html('共搜到相关试题'+ num + '个');
        			return ;
        		}
        		if(exams){  
        			num =  exams.count;
        			total_count = num;
        		}else{
        			num = 0;
        			$('#pullUp').css('display','none');
        			$('#loadingDiv').css('display','none');
        			$('.noSearch').css('display','block');
        			$('.title').html('共搜到相关试题'+ num + '个');
        			return ;
        		}
        		$('.title').html('共搜到相关试题'+ num + '个');
        		list(exams,_curPage,keyWord);	
        		myscroll.refresh();
        		if(exams.count <= 50){
        			$('#pullUp').css('display','none');
        		}else{
        			$('#pullUp').css('display','block');
        		}
				
	        }, 1000);
	    }
	
		
	}
	
	init();
});


function clickTiaozhuan(allTestID,_curPage,_keyWord){
	window.location.href = '/answerExam?fromUrl=searchExam&type=findTest&allTestID=' + allTestID + '&page=' + _curPage + '&keyWord=' + _keyWord;
}

var page = 2;
function pullUpAction(){
	if(total_count <= 50){
		G_Prg.popMsg('试题已加载完毕');
		return;
	}
	var _keyWord = decodeURI(GetQueryString('keyWord'));
	setTimeout(function () {
    	var appEName = G_Cookie.getAppEName();
    	var userName = G_Cookie.getUserName();
        var userID = G_Cookie.getUserID();
		var exams = _getSearchExamJson(appEName, _keyWord, page, userName, userID);
		if(exams == '没有找到相关试题'){
			G_Prg.popMsg('没有找到相关试题');
			$('#loadingDiv').css('display','none');
			$('#pullUp').css('display','none');
			myscroll.refresh();
		}else{
			list(exams,page,_keyWord);
			myscroll.refresh();
			if(page*50 > exams.count){
				myscroll.refresh();
				G_Prg.popMsg('搜索内容已加载完毕');
				$('#pullUp').css('display','none');
			}else{
				page ++;
			}
		}
		
    }, 100);
}

function list(exams,_curPage,keyword){
	var data;
	if(exams.count > 0){
		count = exams.count;
		//data = exams.test.StyleItems;
		var data = G_SetTestInfo.replaceImgURL(exams.test).StyleItems;
		var str = '';
		var startIndex = (_curPage - 1) * 50 + 1
		for(var i = 0; i < data.length; i ++){
			var cptID = '';
			var title = '';
			var alltestid = '';
			if(data[i].Type == 'ATEST'){ // A1/A2题型
				cptID = data[i].TestItems[0].CptID;
				title = data[i].TestItems[0].Title;
				alltestid = data[i].TestItems[0].AllTestID;
			}else if(data[i].Type == 'BTEST'){ // 'B'体型?
				cptID = data[i].TestItems[0].CptID;
				alltestid = data[i].TestItems[0].AllTestID;
				title = data[i].TestItems[0].BTestItems[0].Title;
			}else if(data[i].Type == 'A3TEST'){ //'A3/A4'题型
				cptID = data[i].TestItems[0].CptID;
				alltestid = data[i].TestItems[0].AllTestID;
				title = data[i].TestItems[0].FrontTitle+ '<br/>' + data[i].TestItems[0].A3TestItems[0].Title;
			}else if(data[i].Type == 'XTEST'){
				cptID = data[i].TestItems[0].CptID;
				alltestid = data[i].TestItems[0].AllTestID;
				title = data[i].TestItems[0].Title;
			}else if(data[i].Type == 'JDTEST' || data[i].Type == 'PDTEST' || data[i].Type == 'TKTEST'){
				cptID = data[i].TestItems[0].CptID;
				alltestid = data[i].TestItems[0].AllTestID;
				title = data[i].TestItems[0].Title;
			}
			
			str += '<li alltestid="'+ alltestid +'" page="'+ _curPage +'" key="'+ keyword +'" cptid="'+ cptID +'"><span>'+ (startIndex+i) + '.' + title + '</span></li>';
		}
		$('#loadingDiv').css('display','none');
		$('.exam_ullist').append(str);
		$('.exam_ullist li').unbind('click').click(function(){
			 G_Cookie.setCptID($(this).attr('cptid'));
			clickTiaozhuan($(this).attr('alltestid'),$(this).attr('page'),$(this).attr('key'));
		});
	}else{
		$('#loadingDiv').css('display','none');
		//$('.noSearch').css('display','block');
	}  
}
        		


function _getSearchExamJson(appEName, keyWord, curPage, userName, userID) {
	var searchDataTest = '';
	var URL = '/api/searchTest/getTestByTestKeyApi';
	var params = {
		appEName: appEName,
		keyWord: keyWord,
		curPage: curPage - 1,
		userID: userID,
		userName: userName,
		guid: G_Cookie.getGuid()
	};
	var _success = function(json) {
			G_Prg.$('loadingDiv').style.display = 'block'; //显示加载圈 
			if(!json) {
				G_Prg.throw('程序运行错误，TestListData._getSearchExamJson请求服务器无返回');
			}
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				//接收数据
				searchDataTest = jsonData.data;
				if(searchDataTest.test.StyleItems.length === 0) {
					return;
				}
			} else if(jsonData.status === 201) { //搜索不到题目
				searchDataTest = jsonData.msg;
			} else if(jsonData.status === 202) { //部分题目获取失败
				//G_Prg.throw('程序运行错误，TestListData._getSearchExamJson,部分试题获取失败');
			} else if(jsonData.status === 203) { //用户未购买该科目
				G_Prg.confirm('该功能充值后才能使用！', function() {
					window.location.href = '/recharge';
				});
				return;

			} else if(jsonData.status === 300) {
				//G_maiDian.setBugRecordInfos('程序错误', '查找试题', jsonData.status + ':' + jsonData.msg);
				G_Prg.throw('程序运行错误，TestListData. _getSearchExamJson,数据库获取数据异常');
			} else if(jsonData.status >= 400 && jsonData.status < 500) {
				//G_maiDian.setBugRecordInfos('业务逻辑错误', '查找试题', jsonData.status + ':' + jsonData.msg);
				G_Prg.throw('程序运行错误，TestListData. _getSearchExamJson,参数错误');
			} else {
				//G_maiDian.setBugRecordInfos('其它程序错误', '查找试题', jsonData.status + ':' + jsonData.msg);
				G_Prg.throw('抱歉，TestListData. _getSearchExamJson，不能处理 status = "' + jsonData.status + '"！');
			}
		} 
	G_AjaxApi_Record.post(URL, params, false, _success);
	return searchDataTest;
}

function GetQueryString(name) {  
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
    var r = window.location.search.substr(1).match(reg);  //获取url中"?"符后的字符串并正则匹配
    var context = "";  
    if (r != null)  
         context = r[2];  
    reg = null;  
    r = null;  
    return context == null || context == "" || context == "undefined" ? "" : context;  
}